module.exports = function(window, document) {
	var apivm = require('../../common/avm.min')(window, document);
	var avm = apivm;
	var api = apivm.api;
	var Component = apivm.Component;
	var $ = apivm.$;
	(function() {
	/*
	 * APICloud JavaScript Library
	 * Copyright (c) 2014 apicloud.com
	 */
	var $kn = {
		byId: function byId(id) {
			return document.getElementById(id);
		},
		ajax: function ajax(p, callback) {
			var _this = this;
			var param = p;
			if (!param.headers) {
				param.headers = {};
			}
			param.headers["x-apicloud-mcm-key"] = "cZKzX7DabDmYyfez";
			if (param.data && param.data.body) {
				param.headers["Content-Type"] = "application/json; charset=utf-8";
			}
			if (param.url) {
				var baseUrl = "https://a8888888888888-pd.apicloud-saas.com/api/";
				param.url = baseUrl + param.url;
			}
			api.ajax(param, function(ret, err) {
				if (callback) callback(ret, err);
				if (ret) {
					var status = ret.status;
					if (status && status == 4001) {
						var didShowLogoutAlert = api.getGlobalData({
							key: "didShowLogoutAlert"
						});

						if (!didShowLogoutAlert) {
							api.setGlobalData({
								key: "didShowLogoutAlert",
								value: true
							});

							_this.setUserInfo("");
							api.alert(
								{
									msg: "登录已失效，请重新登录"
								},
								function() {
									api.setGlobalData({
										key: "didShowLogoutAlert",
										value: false
									});

									api.closeToWin({
										name: "root"
									});
								}
							);
						}
					}
				}
			});
		},
		getUserInfo: function getUserInfo() {
			var value = api.getPrefs({
				key: "userInfo",
				sync: true
			});

			return value ? JSON.parse(value) : "";
		},
		setUserInfo: function setUserInfo(userInfo) {
			api.setPrefs({
				key: "userInfo",
				value: userInfo
			});
		},
		getCurrentCityInfo: function getCurrentCityInfo() {
			var value = api.getPrefs({
				key: "currentCity",
				sync: true
			});

			return value ? JSON.parse(value) : "";
		},
		setCurrentCityInfo: function setCurrentCityInfo(cityInfo) {
			api.setPrefs({
				key: "currentCity",
				value: cityInfo
			});
		},
		getWareTypeList: function getWareTypeList() {
			var value = api.readFile({
				sync: true,
				path: "fs://WareTypeList"
			});

			return value ? JSON.parse(value) : "";
		},
		setWareTypeList: function setWareTypeList(list) {
			api.writeFile({
				path: "fs://WareTypeList",
				data: JSON.stringify(list)
			});
		},
		fitRichText: function fitRichText(richtext, width) {
			var str = '<img style="max-width:' + width + 'px;"';
			var result = richtext.replace(/\<img/gi, str);
			return result;
		}
	};

	var StatusBarView = /*@__PURE__*/ (function(Component) {
		function StatusBarView(props) {
			Component.call(this, props);
			this.compute = {
				statusBarHeight: function() {
					if (api.safeArea) {
						return api.safeArea.top;
					} else {
						var res = wx.getSystemInfoSync();
						return res.statusBarHeight;
					}
				}
			};
		}

		if (Component) StatusBarView.__proto__ = Component;
		StatusBarView.prototype = Object.create(Component && Component.prototype);
		StatusBarView.prototype.constructor = StatusBarView;
		StatusBarView.prototype.render = function() {
			return apivm.h("view", {style: "height:" + this.statusBarHeight + "px;"});
		};

		return StatusBarView;
	})(Component);
	apivm.define("status-bar-view", StatusBarView);

	var Navigationbar = /*@__PURE__*/ (function(Component) {
		function Navigationbar(props) {
			Component.call(this, props);
		}

		if (Component) Navigationbar.__proto__ = Component;
		Navigationbar.prototype = Object.create(Component && Component.prototype);
		Navigationbar.prototype.constructor = Navigationbar;
		Navigationbar.prototype.onLeftButton = function(e) {
			api.closeWin();
		};
		Navigationbar.prototype.render = function() {
			return apivm.h(
				"view",
				{class: "nav-container"},
				apivm.h("status-bar-view", null),
				apivm.h(
					"view",
					{class: "nav-header"},
					apivm.h(
						"view",
						{
							class: "nav-header-button nav-left-button",
							onclick: this.props.onLeftButton
								? this.props.onLeftButton
								: this.onLeftButton
						},
						apivm.h("image", {
							width: this.props.leftButtonWidth ? this.props.leftButtonWidth : 11,
							src: this.props.leftButtonIcon
								? this.props.leftButtonIcon
								: "../../image/back.png",
							mode: "widthFix"
						}),
						apivm.h("text", {class: "nav-header-text"}, this.props.leftButtonText)
					),
					apivm.h("text", {class: "nav-header-title"}, this.props.title),
					apivm.h(
						"view",
						{
							class: "nav-header-button nav-right-button",
							onclick: this.props.onRightButton
						},
						apivm.h("image", {
							width: this.props.rightButtonWidth ? this.props.rightButtonWidth : 0,
							src: this.props.rightButtonIcon ? this.props.rightButtonIcon : "",
							mode: "widthFix"
						}),
						apivm.h("text", {class: "nav-header-text"}, this.props.rightButtonText)
					)
				)
			);
		};

		return Navigationbar;
	})(Component);
	apivm.define("navigationBar", Navigationbar);

	var Address = /*@__PURE__*/ (function(Component) {
		function Address(props) {
			Component.call(this, props);
			this.data = {
				haveData: false,
				dataList: []
			};
		}

		if (Component) Address.__proto__ = Component;
		Address.prototype = Object.create(Component && Component.prototype);
		Address.prototype.constructor = Address;
		Address.prototype.apiready = function() {
			var that = this;
			api.addEventListener(
				{
					name: "updateAddress"
				},
				function(ret, err) {
					that.getAddressList();
				}
			);
			this.getAddressList();
		};
		Address.prototype.getAddressList = function() {
			api.showProgress({
				title: "",
				text: ""
			});

			var that = this;
			$kn.ajax(
				{
					url: "addresses/getAddressList"
				},
				function(ret, err) {
					if (ret && ret.data && ret.data.length > 0) {
						var data = ret.data;
						for (var i = 0; i < data.length; i++) {
							var item = data[i];
							if (item.type == 0) {
								item.typeDesc = "公司";
							} else if (item.type == 1) {
								item.typeDesc = "住宅";
							} else if (item.type == 2) {
								item.typeDesc = "学校";
							} else if (item.type == 3) {
								item.typeDesc = "其它";
							}
						}
						that.data.haveData = true;
						that.data.dataList = data;
					} else {
						that.data.haveData = false;
					}
					api.hideProgress();
				}
			);
		};
		Address.prototype.onRightButton = function() {
			this.openSetAddressWin();
		};
		Address.prototype.onItem = function(e) {
			this.openSetAddressWin(e.currentTarget.dataset.item);
		};
		Address.prototype.openSetAddressWin = function(pageParam) {
			api.openWin({
				name: "setaddress",
				url: "../setaddress/setaddress.stml",
				pageParam: pageParam ? pageParam : {}
			});
		};
		Address.prototype.render = function() {
			var this$1 = this;
			return apivm.h(
				"view",
				{class: "main"},
				apivm.h("navigationBar", {
					title: "收货地址",
					rightButtonText: "添加",
					onRightButton: this.onRightButton
				}),
				apivm.h(
					"view",
					{class: "content"},
					this.data.haveData
						? apivm.h(
								"scroll-view",
								{class: "listView", "scroll-y": "true"},
								Object.entries(this.data.dataList).map(function(ref) {
									var index$1 = ref[0];
									var item$1 = ref[1];
									return apivm.h(
										"view",
										{class: "cell", "data-item": item$1, onclick: this$1.onItem},
										apivm.h(
											"view",
											{class: "left"},
											apivm.h("image", {
												class: "icon",
												src: "../../image/icon_address.png"
											}),
											apivm.h(
												"text",
												{class: "defaultDesc"},
												item$1.isDefault == 1 ? "[默认]" : ""
											)
										),
										apivm.h(
											"view",
											{class: "middle"},
											apivm.h(
												"text",
												{class: "middle-top"},
												"收货人：" + item$1.name + "  " + item$1.mobile
											),
											apivm.h(
												"view",
												{class: "middle-bottom"},
												apivm.h("text", {class: "typeDesc"}, "[" + item$1.typeDesc + "]"),
												apivm.h(
													"text",
													{class: "address"},
													item$1.region + item$1.address + item$1.house
												)
											)
										),
										apivm.h("image", {
											class: "right",
											src: "../../image/item_user_address_right.png"
										})
									);
								})
						  )
						: apivm.h(
								"view",
								{id: "empty", class: "empty"},
								apivm.h("image", {
									class: "empty-icon",
									src: "../../image/address_empty.png"
								})
						  )
				)
			);
		};

		return Address;
	})(Component);
	apivm.define("address", Address);
	apivm.render(apivm.h("address", null), "body");
})();

	window['createApp'] = function() {
		apivm.$_start();
	}
	return avm;
}